//
//  weiboController.m
//  weiAai
//
//  Created by ibokan on 14-12-15.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//

#import "weiboController.h"
#import "AFNetworking.h"
#import "zhuceController.h"
#import "yanzhengController.h"
#import "firstViewController.h"
@interface weiboController ()<UIWebViewDelegate>
@property(assign,nonatomic) int client_id;
@property(copy,nonatomic) NSString  *access_token;
@end

@implementation weiboController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupWeibo];

}
-(void)setupWeibo
{
    
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"access_token"];
    NSLog(@"%@",token);
//    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userID"];
    if (token != nil) {
        yanzhengController *zhuce =[[yanzhengController alloc]init];
        [self.navigationController pushViewController:zhuce animated:YES];
        
//        return;
    }else{
        UIWebView *web=[[UIWebView alloc]init];
        web.frame =self.view.bounds;
        [self.view addSubview:web];
    //https://api.weibo.com/oauth2/default.html

        NSURL *url =[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=1057603691&redirect_uri=https://api.weibo.com/oauth2/default.html"];
        //设置代理截取web加载的地址，获取code
        web.delegate =self;
        NSURLRequest *request =[NSURLRequest requestWithURL:url];
        [web loadRequest:request];
    }
}
#pragma make web代理
//webView发送请求之前都会调用这个方法,询问代理可不可以加载这个页面
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
   
    
    
    
    
    //absoluteString拿出URL的绝对路径
    NSString *urlString = request.URL.absoluteString;
    NSRange range = [urlString rangeOfString:@"code="];
    if (range.location !=NSNotFound) {
        int loc = (int )(range.location+range.length);
    NSString *code = [urlString substringFromIndex:loc];
        
        //发送请求获取tokon
        AFHTTPRequestOperationManager*mg =[AFHTTPRequestOperationManager  manager];
        mg.responseSerializer =[AFJSONResponseSerializer serializer];
        NSMutableDictionary *dic =[NSMutableDictionary dictionary];
        dic[@"client_id"]=@"1057603691";
              dic[@"client_secret"]=@"0a4ec00eb374a28ffa58095c4a9b24cf";
              dic[@"grant_type"]=@"authorization_code";
              dic[@"code"]=code;
                dic[@"redirect_uri"]=@"https://api.weibo.com/oauth2/default.html";
       [mg POST:@"https://api.weibo.com/oauth2/access_token" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
           
           NSString *accesstoken =responseObject[@"access_token"];
           NSLog( @"%@,%@",responseObject,accesstoken);
           NSString *userID = responseObject[@"uid"];
           NSLog(@"%@",userID);
           
           //归档
          // NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
           //NSString *file =[doc stringByAppendingString:@"token.app"];
           [[NSUserDefaults standardUserDefaults]setObject:accesstoken forKey:@"access_token"];
           [[NSUserDefaults standardUserDefaults]setObject:userID forKey:@"userID"];
           
           
           yanzhengController *zhuce =[[yanzhengController alloc]init];
           [self.navigationController pushViewController:zhuce animated:YES];
           //self.view.window.rootViewController = zhuce;
          
           
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           
       }];
    }
    
    return YES;
}
@end
